<?php
session_start();

mysql_connect("127.0.0.1","root","sd2013");
mysql_select_db("mathRomp");

$url = 'http://localhost/mathRomp/teacherHome.html';
$numStudents = $_SESSION['numStudents'];
$numQuestions = $_SESSION['numQuestions'];
$assignId = $_SESSION['assignId'];
$assignment = $_SESSION['assignment'];
$userId = -1;

if($_POST['add'] == "submit"){
  $continue = 0;
  for($i=0; $i<$numStudents; $i++){
    $q=mysql_query("SELECT `userId` FROM `Users` WHERE `username`='".$_REQUEST['student'.$i.'']."'");
    while($e=mysql_fetch_assoc($q)){
      $output=$e;
    }
    if($output == null){
      $continue = 1;
      echo '<p style="color:red">'.$_REQUEST['student'.$i.''].' is not a valid username</p>';
      break;
    }else{
      $user = json_encode($output);
      $user2 = json_decode($user);
      //$q2=mysql_query("SELECT * FROM `Assignment` WHERE `userId` = ".$user2->userId." AND `assignmentId` = ".$assignId."");
      //if($q2 == null){
	$q3=mysql_query("INSERT INTO `Assignment`(`assignmentId`, `userId`, `numProblems`, `templates`, `problems`, `currentProbNum`, `results`, `percentage`) VALUES (".$assignId.",".$user2->userId.",".$numQuestions.",'".$assignment."', '', 0, 0, 0)");
	//}
    }
  }
  if($continue == 0){
    header("Location: $url");
  }
}

echo '<html>
   <head>
      <title>Create New Assignment</title>
   </head>
   <body>
      <h1>Create New Assignment</h1>
       <form action="addUsers.php" method="post" name="maint"
   id="maint">
         <fieldset class="maintform">
            <legend>Add Students</legend>';

for($i=0; $i<$numStudents; $i++){
  echo '<li><label class="required">Student '.($i+1).'</label><br />
<input type="text" name="student'.$i.'" id="student'.$i.'" class="required"/></li><br />';
}


echo '<button type="submit" value="submit" name="add">Next</button>
      </form>
      </body>
      </html>';

mysql_close();